package com.dhecp.project.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.business.domain.ProductGNumber;
import com.dhecp.project.business.service.ProductGNumberService;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 型号条码记录 控制层
 * @author DH-Xxj
 * @version 2021年3月30日
 */
@Controller
@RequestMapping("/business/productgnumber")
public class ProductGNumberController extends BaseModuleController{

//	private String prefix = "business/productgnumber";
    private String moduleTable = ModuleTable.PRODUCT_GNUMBER;
    
    @Autowired
    private ProductGNumberService productGNumberService;
        
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
     * 打开型号条码库查询列表
     * @param 
     * @return ModelAndView
     * @author DH-Xxj
     */
	@RequiresPermissions("business:productgnumber:view")
    @GetMapping()
	public ModelAndView listPage(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductGNumber()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("prefix","business/business"); //页面路径
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("business", "productgnumber");
		return mv;
    }
	
	/**
     * 查询型号条码库信息
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
	@GetMapping("/list")
    @RequiresPermissions("business:productgnumber:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<ProductGNumber> list = new ArrayList<ProductGNumber>();
        try {
        	list = productGNumberService.queryProductGNumberList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	/**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productgnumber:export")
    @Log(title = "型号条码库导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductGNumber()); 
        List<ProductGNumber> list = productGNumberService.queryProductGNumberList(params);
        params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductGNumber> util = new ExportExcelUtils<ProductGNumber>(moduleTable,new ProductGNumber());
        return util.exportExcel(list, fieldList, "型号条码库数据", "export");
    }
    
    /**
     * 导出Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productgnumber:import")
    @Log(title = "型号条码库导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplate(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductGNumber()); 
    	params.put("getFieldAttributesType", "template");

    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductGNumber> util = new ExportExcelUtils<ProductGNumber>(moduleTable,new ProductGNumber());
        return util.exportExcel(null, fieldList, "型号条码库导入模板", "template");
    }

    /**
     * 新增型号条码信息——数据提交
     * @param params
     * @return AjaxResult
     */
    @RequiresPermissions("business:productgnumber:add")
    @Log(title = "新增型号条码", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult productGNumberAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productGNumberService.insertProductGNumberInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
